//
//  MainPageViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/2.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "MainPageViewController.h"
#import "DropDownViewController.h"
#import "BackgroundViewController.h"
#import "Watch.h"
#import "DataManager.h"
@interface MainPageViewController ()
@property(nonatomic) BOOL is_open;
@property(nonatomic,strong)  UIWindow *keyWindow;
@property(nonatomic,strong)  UIWindow *alertView;
@property(nonatomic,strong)  UIWindow *alertView1;;
@property(nonatomic,strong) DropDownViewController *dropView;
@property(nonatomic,strong) BackgroundViewController *backView;
@property(nonatomic,strong) UILabel *titleLable;
@property(nonatomic,strong) UIImageView *listIcon;
@property(nonatomic,strong) Watch *selectWatch;
@property(nonatomic,assign) float rate;
@end

@implementation MainPageViewController
@synthesize titleLable;
@synthesize listIcon;
@synthesize rate;
-(void)dealloc
{
    self.keyWindow=NULL;
    self.alertView=NULL;
    self.alertView1=NULL;
    self.backView=NULL;
    self.dropView=NULL;
    self.titleLable=NULL;
    self.listIcon=NULL;
    [self removeObservers];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.extendedLayoutIncludesOpaqueBars=YES;
    [self registerObservers];
    float mainWidth=[UIScreen mainScreen].bounds.size.width;
    float mainHeight=[UIScreen mainScreen].bounds.size.height;
    rate=mainWidth/375;
    //初始化导航栏 titleView
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(openWatch:)];
    tap.delegate=self;
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake((mainWidth-200*rate)/2, 0, 200*rate, 64*rate)];
    view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    view.autoresizesSubviews = YES;
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
    {
        NSLog(@"MainPageViewController,viewDidLoad,dataManager is null.");
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
    {
        NSLog(@"MainPageViewController,viewDidLoad,user is null.");
        return;
    }
    self.selectWatch=[dataManager getActiveWatch:user.uid];
    int num=3;
    if(self.selectWatch!=NULL&&self.selectWatch.nickname!=NULL&&![self.selectWatch.nickname isEqualToString:@"(null)"])
    {
        if(self.selectWatch.nickname.length>3)
            num=self.selectWatch.nickname.length;
    }
    titleLable=[[UILabel alloc] initWithFrame:CGRectMake((200-num*20)*rate/2, 0, (num*20)*rate, 64*rate)];
    //lab.font=[UIFont systemFontOfSize:18.0];
    titleLable.textColor=[UIColor whiteColor];
    titleLable.adjustsFontSizeToFitWidth=NO;
    titleLable.textAlignment=UITextAlignmentCenter;
    listIcon=[[UIImageView alloc] initWithFrame:CGRectMake((200-num*20)*rate/2+(num*20)*rate, 64*rate/2, 18, 8)];
    listIcon.image=[UIImage imageNamed:@"list_icon_close@2x.png"];
    [view addSubview:titleLable];
    [view addSubview:listIcon];
    view.userInteractionEnabled=YES;
    [view addGestureRecognizer:tap];
    self.navigationItem.titleView=view;
    //标题赋值

    if(self.selectWatch!=NULL&&self.selectWatch.nickname!=NULL&&![self.selectWatch.nickname isEqualToString:@"(null)"])
    {
        titleLable.text=self.selectWatch.nickname;
    }
    else if(self.selectWatch!=NULL)
    {
        titleLable.text=[NSString stringWithFormat:@"手表%@",self.selectWatch.watchId];
    }
    //初始化弹出层
    self.is_open=NO;
    self.alertView=[[UIWindow alloc]initWithFrame:CGRectMake(0, 0, mainWidth, mainHeight)];
    self.alertView1=[[UIWindow alloc]initWithFrame:CGRectMake(0, 0, mainWidth, mainHeight)];
    self.dropView=[[DropDownViewController alloc] init];
    self.dropView.motherNav=self;
    self.backView=[[BackgroundViewController alloc] init];

    self.keyWindow = [[[UIApplication sharedApplication] delegate] window];
    self.alertView1.rootViewController=self.backView;
    [self.alertView1 setWindowLevel:UIWindowLevelAlert];
    
    [self.dropView setMotherLabel:titleLable];
    [self.dropView setMotherWindow:self.alertView];
    self.alertView.rootViewController=self.dropView;
    [self.alertView setWindowLevel:UIWindowLevelAlert];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}
- (IBAction)openWatch:(UITapGestureRecognizer *)sender
{
    if(!self.is_open)
    {
        [self.keyWindow addSubview:self.alertView1];
        [self.keyWindow addSubview:self.alertView];
        [self.alertView1 makeKeyAndVisible];
        [self.alertView makeKeyAndVisible];
        self.is_open=YES;
        listIcon.image=[UIImage imageNamed:@"list_icon_open@2x.png"];
    }
    else
    {
        [self dismiss];
    }

}
-(void)dismiss
{
    self.alertView.hidden=YES;
    self.alertView1.hidden=YES;
    [self.alertView removeFromSuperview];
    [self.alertView1 removeFromSuperview];
    self.is_open=NO;
    listIcon.image=[UIImage imageNamed:@"list_icon_close@2x.png"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)registerObservers
{
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:CHANGEBABYINFO object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:UNBINDDEVICE object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:USERINFOUPDATE object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:CHANGEWATCH object:nil];
}
-(void)removeObservers
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CHANGEBABYINFO object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UNBINDDEVICE object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:USERINFOUPDATE object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CHANGEWATCH object:nil];
}
- (void)receiveNotification:(NSNotification *)sender {
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
        return;
    User *user=[User sharedSingleton];
    if(user==NULL)
        return;
   self.selectWatch=[dataManager getActiveWatch:user.uid];
    if(self.selectWatch.nickname!=NULL&&![self.selectWatch.nickname isEqualToString:@"(null)"])
    {
        if([sender.name isEqualToString:CHANGEBABYINFO]||[sender.name isEqualToString:USERINFOUPDATE])
        {
            [self updateTitleLable];
            
        }
        else if([sender.name isEqualToString:UNBINDDEVICE]||[sender.name isEqualToString:CHANGEWATCH])
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self updateTitleLable];
            });
        }


    }
    
    
}
-(void)updateTitleLable
{
    int num=3;
    if(self.selectWatch!=NULL&&self.selectWatch.nickname!=NULL&&![self.selectWatch.nickname isEqualToString:@"(null)"])
    {
        if(self.selectWatch.nickname.length>3)
            num=self.selectWatch.nickname.length;
    }
    titleLable.frame=CGRectMake((200-num*20)*rate/2, 0, (num*20)*rate, 64*rate);
    listIcon.frame=CGRectMake((200-num*20)*rate/2+(num*20)*rate, 64*rate/2, 18, 8);
    self.titleLable.text=self.selectWatch.nickname;
    
    //***切换手表刷新头像

    
}
-(void)updateTitleLable:(NSString *)titleStr
{
    int num=3;
    if(titleStr!=NULL&&![titleStr isEqualToString:@"(null)"])
    {
        if(titleStr.length>3)
            num=titleStr.length;
    }
    titleLable.frame=CGRectMake((200-num*20)*rate/2, 0, (num*20)*rate, 64*rate);
    listIcon.frame=CGRectMake((200-num*20)*rate/2+(num*20)*rate, 64*rate/2, 18, 8);
    self.titleLable.text=titleStr;
}
@end
